package com.yanqu.road.server.manager.unioncampwar;

import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.unioncampwar.UnionCampWarHoldData;
import com.yanqu.road.entity.unioncampwar.UnionCampWarUserData;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manger.ConfigMgr;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarCampInfo {
    private int campId;
    /**
     * 阵营血量为所有据点血量和
     */
    private int leftHp;
    private int totalHp;
    private long lastWinTime;
    private Map<Integer, UnionCampWarHoldData> holdInfoMap = new ConcurrentHashMap<>();
    private Map<Long, UnionCampWarUserData> userDataMap = new ConcurrentHashMap<>();
    /**
     * 宝库公告数据
     */
    private List<UnionCampWar.UnionCampBoxNoticeTemp> boxNoticeMsgList = new ArrayList<>();
    /**
     * 阵营聊天
     */
    private WorldMessageProto.WorldMessageListMsg.Builder chatListMsg = WorldMessageProto.WorldMessageListMsg.newBuilder();

    /**
     * 榜单数据,领奖期才需要,所以可以固定下来
     */
    private List<UnionCampWar.UnionCampWarUserScoreRankTemp> rankMsgList = new ArrayList<>();

    public UnionCampWarCampInfo(int campId) {
        this.campId = campId;
    }

    public void addHoldInfo(UnionCampWarHoldData holdInfo) {
        holdInfoMap.put(holdInfo.getHoldId(), holdInfo);
    }

    public void addUserData(UnionCampWarUserData userData) {
        userDataMap.put(userData.getUserId(), userData);
    }

    public int getCampId() {
        return campId;
    }

    public void setCampId(int campId) {
        this.campId = campId;
    }

    public Map<Integer, UnionCampWarHoldData> getHoldInfoMap() {
        return holdInfoMap;
    }

    public void setHoldInfoMap(Map<Integer, UnionCampWarHoldData> holdInfoMap) {
        this.holdInfoMap = holdInfoMap;
    }

    public UnionCampWarHoldData getHoldInfo(int holdId) {
        return holdInfoMap.get(holdId);
    }

    public int getLeftHp() {
        return leftHp;
    }

    public int getFirstHoldLeftHp(){
        return holdInfoMap.get(1).getLeftHp();
    }

    public long getFirstHoldLastBattleTime(){
        return holdInfoMap.get(1).getLastBattleTime();
    }

    public void setLeftHp(int leftHp) {
        this.leftHp = leftHp;
    }

    public int getTotalHp() {
        return totalHp;
    }

    public void setTotalHp(int totalHp) {
        this.totalHp = totalHp;
    }

    public void initCampHp() {
        this.leftHp = 0;
        this.totalHp = 0;

        ArrayList<UnionCampWarHoldData> holdList = new ArrayList<>(holdInfoMap.values());
        for (UnionCampWarHoldData holdInfo : holdList) {
            this.leftHp += holdInfo.getLeftHp();
            this.totalHp += holdInfo.getTotalHp();
        }
    }

    public UnionCampWar.UnionCampWarCampInfoTemp.Builder parseCampMsg() {
        UnionCampWar.UnionCampWarCampInfoTemp.Builder campMsg = UnionCampWar.UnionCampWarCampInfoTemp.newBuilder();
        campMsg.setCampId(this.getCampId());
        campMsg.setLeftHp(this.getLeftHp());
        campMsg.setTotalHp(this.getTotalHp());

        for (UnionCampWarHoldData holdInfo : getHoldInfoMap().values()) {
            UnionCampWar.UnionCampWarHoldInfoTemp.Builder holdMsg = parseHoldMsg(holdInfo);
            campMsg.addHoldInfo(holdMsg);
        }
        return campMsg;
    }

    public UnionCampWar.UnionCampWarHoldInfoTemp.Builder parseHoldMsg(UnionCampWarHoldData holdData) {
        UnionCampWar.UnionCampWarHoldInfoTemp.Builder builder = UnionCampWar.UnionCampWarHoldInfoTemp.newBuilder();
        builder.setHoldId(holdData.getHoldId());
        builder.setLeftNum(holdData.getLeftHp());
        builder.setRightNum(holdData.getTotalHp());
        builder.setLastBattleTime(holdData.getLastBattleTime());
        builder.setUnionId(holdData.getUnionId());
        builder.setPatronsNum(holdData.getPatronsNum());
        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(holdData.getUnionId());
        if (unionInfo != null) {
            builder.setUnionBaseInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
        } else {
            unionInfo = CrossUnionMgr.getDeleteUnionInfoMap().get(holdData.getUnionId());
            builder.setUnionBaseInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
        }

        return builder;
    }

    public List<UnionCampWarUserData> getCampMemberScoreRankList() {
        List<UnionCampWarUserData> dataList = getAllCampUserData();
        dataList.sort((o1, o2) -> {
            int compare = Integer.compare(o2.getScore(), o1.getScore());
            if (0 == compare) {
                compare = Long.compare(o1.getLastAttackTime(), o2.getLastAttackTime());
            }
            return compare;
        });
        return dataList;
    }

    public int getUserRank(long userId) {
        List<UnionCampWarUserData> rankList = getCampMemberScoreRankList();
        for (int i = 0; i < rankList.size(); i++) {
            if (rankList.get(i).getUserId() == userId) {
                return i + 1;
            }
        }
        return -1;
    }

    public UnionCampWar.UnionCampBoxNoticeTemp getLastBoxNoticeMsg() {
        if (boxNoticeMsgList.isEmpty()) {//没有公告返回空
            return null;
        }
        synchronized (boxNoticeMsgList) {
            return boxNoticeMsgList.get(boxNoticeMsgList.size() - 1);
        }
    }

    public void updateLastWinTime(long time) {
        if (time > this.lastWinTime) {
            this.lastWinTime = time;
        }
    }

    public long getLastWinTime() {
        return lastWinTime;
    }

    public List<UnionCampWarUserData> getAllCampUserData() {
        List<UnionCampWarUserData> allUserList = new ArrayList<>(userDataMap.values());
        List<UnionCampWarUserData> haveScoreList = new ArrayList<>();
        for (UnionCampWarUserData warUserData : allUserList) {
            if (warUserData.getScore() > 0) {
                haveScoreList.add(warUserData);
            }
        }
        return haveScoreList;
    }

    public void addBoxNoticeMsg(UnionCampWar.UnionCampBoxNoticeTemp noticeTemp) {
        synchronized (boxNoticeMsgList) {
            boxNoticeMsgList.add(noticeTemp);
            while (boxNoticeMsgList.size() > ConfigMgr.getUnionCampWarConfig().getNoticeNumMax()) {
                boxNoticeMsgList.remove(0);
            }
        }
    }

    public List<UnionCampWar.UnionCampBoxNoticeTemp> getBoxNoticeMsgList() {
        synchronized (boxNoticeMsgList) {
            List<UnionCampWar.UnionCampBoxNoticeTemp> list = new ArrayList<>(boxNoticeMsgList);
            return list;
        }
    }

    /**
     * 结算更新玩家排名
     */
    public void updateUserRank() {
        List<UnionCampWarUserData> rankList = getCampMemberScoreRankList();
        for (int i = 0; i < rankList.size(); i++) {
            int rank = i + 1;
            rankList.get(i).setRank(rank);
        }
    }

    public void addChatMsg(WorldMessageProto.WorldMessageTempMsg chatMsg) {
        synchronized (chatListMsg) {
            chatListMsg.addDataList(chatMsg);

            if (chatListMsg.getDataListCount() >= ConfigMgr.getUnionCampWarConfig().getChatNumMax()) {
                chatListMsg.removeDataList(0);
            }
        }
    }

    public WorldMessageProto.WorldMessageListMsg getChatListMsg() {
        synchronized (chatListMsg) {
            return chatListMsg.build();
        }
    }

    public boolean isHoldAlive() {
        for (UnionCampWarHoldData holdData : holdInfoMap.values()) {
            if (holdData.getLeftHp()>0){
                return true;
            }
        }
        return false;
    }

    public List<UnionCampWar.UnionCampWarUserScoreRankTemp> getRankMsgList() {
        return rankMsgList;
    }

    public synchronized void setRankMsgList(List<UnionCampWar.UnionCampWarUserScoreRankTemp> rankMsgList) {
        this.rankMsgList = rankMsgList;
    }
}
